public class CaseOwnerDepartment 
{
	//anti-recursion classes
	private static boolean alreadyAssignedDepartment = false;
    public static boolean hasAlreadyAssignedDepartment() 
    {
    	return alreadyAssignedDepartment;
    }
    public static void setAlreadyAssignedDepartment() 
    {
    	alreadyAssignedDepartment = true;
    }
    //end anti-recursion classes
    
	//This class is designed to ensure the case assigned group correctly reflected the owner's department.
	public static void assignDepartment(case[] cases)
	{
		if (!CaseOwnerDepartment.hasAlreadyAssignedDepartment()) 
		{
			List<case> updateCases = new List<case>();
			Set<id> caseIdSet = New Set<id>();
			Set<id> ownerIdSet = New set<id>();
			for (integer i = 0; i < cases.size(); i++)
			{
					ownerIdSet.add(cases[i].OwnerId);
					caseIdSet.add(cases[i].id);			
			}
			if (caseIdSet.size()>0)
			{
				Map<id,string> userDeptMap = New Map<id,string>();
				for(user u:[select Id,Assigned_Group_for_Support_Cases__c from user where id in :ownerIdSet])
				{
					userDeptMap.put(u.Id,u.Assigned_Group_for_Support_Cases__c);
				}
				//must create userDeptMap entries for each queue. The Id's are from the Groups table.
				//Hardware Support Team
				userDeptMap.put('00G80000001CL3zEAG','Hardware Support');
				//Software Support Team
				userDeptMap.put('00G80000001UCHeEAO','Software Support');			
				//License Key Team
				userDeptMap.put('00G80000001WyutEAC','License Key Admin');	
				for(case c : [select owner.name,Assigned_Group_1__c from case where id in :caseIdSet])
				{
					if ((userDeptMap.get(c.OwnerId)!=null)&(c.Assigned_Group_1__c != userDeptMap.get(c.OwnerId)))
					{
						c.Assigned_Group_1__c = userDeptMap.get(c.OwnerId);
						updateCases.add(c);
					}
					//create catch-all for users that don't have an assigned group configured on their user record
					else if((userDeptMap.get(c.OwnerId)==null)&(c.Assigned_Group_1__c != 'Software Support'))
					{
						c.Assigned_Group_1__c = 'Software Support';
					}
				}
				update updateCases;
				setAlreadyAssignedDepartment();
			}
		}	
	
	}

}